{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "16d39dfe-d130-4719-8062-f4fa5315eb2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">Model: \"sequential_6\"</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1mModel: \"sequential_6\"\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
       "┃<span style=\"font-weight: bold\"> Layer (type)                    </span>┃<span style=\"font-weight: bold\"> Output Shape           </span>┃<span style=\"font-weight: bold\">       Param # </span>┃\n",
       "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
       "│ embedding_6 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Embedding</span>)         │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">400</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)        │         <span style=\"color: #00af00; text-decoration-color: #00af00\">3,200</span> │\n",
       "└─────────────────────────────────┴────────────────────────┴───────────────┘\n",
       "</pre>\n"
      ],
      "text/plain": [
       "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
       "┃\u001b[1m \u001b[0m\u001b[1mLayer (type)                   \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape          \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m      Param #\u001b[0m\u001b[1m \u001b[0m┃\n",
       "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
       "│ embedding_6 (\u001b[38;5;33mEmbedding\u001b[0m)         │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m400\u001b[0m, \u001b[38;5;34m32\u001b[0m)        │         \u001b[38;5;34m3,200\u001b[0m │\n",
       "└─────────────────────────────────┴────────────────────────┴───────────────┘\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Total params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">3,200</span> (12.50 KB)\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m3,200\u001b[0m (12.50 KB)\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">3,200</span> (12.50 KB)\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m3,200\u001b[0m (12.50 KB)\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Non-trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (0.00 B)\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "embeddings矩阵=\n",
      " [array([[-0.02486333, -0.04672209, -0.00127151, ...,  0.0001465 ,\n",
      "        -0.0118902 , -0.00655659],\n",
      "       [ 0.02424424, -0.038024  , -0.00911429, ...,  0.007151  ,\n",
      "        -0.0364278 , -0.02248389],\n",
      "       [ 0.00675653, -0.04905795,  0.00023402, ...,  0.01485357,\n",
      "         0.01217054,  0.04035293],\n",
      "       ...,\n",
      "       [-0.00244129, -0.03040012,  0.00813819, ...,  0.01571453,\n",
      "        -0.01378268,  0.0002188 ],\n",
      "       [-0.0294003 ,  0.04926466, -0.00690274, ...,  0.02186164,\n",
      "         0.00665834,  0.02108505],\n",
      "       [-0.03149898, -0.0065489 ,  0.03582357, ..., -0.00943834,\n",
      "        -0.04663293,  0.03099566]], dtype=float32)]\n",
      "\u001b[1m3/3\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 19ms/step\n",
      "输出矩阵的形状= (69, 400, 32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "model=tf.keras.Sequential()#构建空的网络模型\n",
    "#创建嵌入层\n",
    "embedding=tf.keras.layers.Embedding(output_dim=32,input_dim=100,input_length=400,input_shape=(400,))\n",
    "model.add(embedding)#添加到神经网络model中\n",
    "model.summary()#显示网络模型的参数信息\n",
    "#显示embeddings矩阵的值\n",
    "print(\"embeddings矩阵=\\n\",embedding.get_weights())\n",
    "text=\"Deep learning is an important concept raised by the current sciences.\"\n",
    "#定义分词对象\n",
    "token=tf.keras.preprocessing.text.Tokenizer(num_words=100)\n",
    "token.fit_on_texts(text)#分词\n",
    "input=token.texts_to_sequences(text)#输出向量序列\n",
    "#序列填充\n",
    "test_seq=tf.keras.preprocessing.sequence.pad_sequences(input,padding='post',maxlen=400,truncating='post')\n",
    "#使用向量序列应用网络模型\n",
    "output_array=model.predict(test_seq)\n",
    "#显示\n",
    "print(\"输出矩阵的形状=\",output_array.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6f3ab106-6743-4f61-882d-a0e2198bfc98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "填充前序列为\n",
      " [[1], [2, 3], [4, 5, 6]]\n",
      "填充前序列为\n",
      " [[1 0 0]\n",
      " [2 3 0]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf#导库\n",
    "s=[[1],[2,3],[4,5,6]]#初始化填充前序列\n",
    "print(\"填充前序列为\\n\",s)#显示填充前序列\n",
    "#填充序列并赋值给a\n",
    "a=tf.keras.preprocessing.sequence.pad_sequences(s,padding='post')\n",
    "print(\"填充前序列为\\n\",a)#显示填充后序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e8ae2dc8-d28f-421d-b63e-c800bc44165b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "剪裁前序列为\n",
      " [[1, 2, 3, 9], [4, 5, 6], [7, 8]]\n",
      "剪裁前序列为\n",
      " [[3 9]\n",
      " [5 6]\n",
      " [7 8]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "s=[[1,2,3,9],[4,5,6],[7,8]]\n",
    "print(\"剪裁前序列为\\n\",s)\n",
    "a=tf.keras.preprocessing.sequence.pad_sequences(s,maxlen=2)\n",
    "print(\"剪裁前序列为\\n\",a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10f1ca91-0375-4c78-8bac-c666605e2c9b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
